using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ReadWriteCsv;
using UnityEngine;

public static class Locale
{
	static Locale()
	{
		Locale.LoadStringTable("platformCommonText");
		Locale.LoadStringTable("StringTable");
	}

	public static LocaleType current
	{
		get
		{
			if (Locale._current == LocaleType.MAX_LOCALE)
			{
				LocaleType current = LocaleType.EN;
				SystemLanguage systemLanguage = Application.systemLanguage;
				if (systemLanguage != SystemLanguage.Danish)
				{
					if (systemLanguage != SystemLanguage.German)
					{
						if (systemLanguage != SystemLanguage.Norwegian)
						{
							if (systemLanguage == SystemLanguage.Swedish)
							{
								current = LocaleType.SE;
							}
						}
						else
						{
							current = LocaleType.NO;
						}
					}
					else
					{
						current = LocaleType.DE;
					}
				}
				else
				{
					current = LocaleType.DK;
				}
				Locale._current = current;
			}
			return Locale._current;
		}
	}

	public static string name
	{
		get
		{
			return Locale.current.ToString();
		}
	}

	public static void ReloadLocale()
	{
	}

	private static bool LoadStringTable(string resName)
	{
		TextAsset textAsset = Resources.Load(resName, typeof(TextAsset)) as TextAsset;
		if (textAsset == null)
		{
			UnityEngine.Debug.LogWarning("stringTable [" + resName + "] not found");
			return false;
		}
		using (CsvFileReader csvFileReader = new CsvFileReader(new MemoryStream(Encoding.UTF8.GetBytes(textAsset.text))))
		{
			string name = Locale.name;
			CsvRow csvRow = new CsvRow();
			csvFileReader.ReadRow(csvRow);
			List<string> list = new List<string>(csvRow);
			while (csvFileReader.ReadRow(csvRow))
			{
				if (csvRow[0] == name)
				{
					break;
				}
			}
			int i;
			for (i = 1; i < csvRow.Count; i++)
			{
				Locale.stringTable.Add(list[i], csvRow[i]);
			}
			if (list.Count != csvRow.Count)
			{
				UnityEngine.Debug.LogError("Miss match Locale String Table : " + list[i]);
			}
		}
		return true;
	}

	public static string GetString(this string key)
	{
		if (Locale.stringTable == null)
		{
			UnityEngine.Debug.LogError("StringTable is null");
			return "<StringTable not found>";
		}
		if (key == null)
		{
			return null;
		}
		if (Locale.stringTable.ContainsKey(key))
		{
			return Locale.stringTable[key];
		}
		return key;
	}

	private static LocaleType _current = LocaleType.MAX_LOCALE;

	private static Dictionary<string, string> stringTable = new Dictionary<string, string>();
}
